﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;

namespace KTX
{
    public partial class frmKhoiTaoDienNuoc : DevExpress.XtraEditors.XtraForm
    {
        public frmKhoiTaoDienNuoc()
        {
            InitializeComponent();
        }

        Data.QLKTXEntities db = new Data.QLKTXEntities();

        private void frmKhoiTaoDienNuoc_Load(object sender, EventArgs e)
        {
            phongBindingSource.DataSource = db.Phong;
            this.dienNuocBindingSource.DataSource = db.DienNuoc.Where(p=>p.MaDienNuoc==-1);
        }

        private void barThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.dienNuocBindingSource.AddNew();
        }

        private void barXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void barSua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void barLuu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            db.SaveChanges();
        }

        private void barIn_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void barDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void phongBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            if (phongBindingSource.Current!=null)
            {
                var phong = (Data.Phong)phongBindingSource.Current;
                dienNuocBindingSource.DataSource = db.DienNuoc.Where(p=>p.MaPhong == phong.MaPhong);
            }
        }

        private void dienNuocBindingSource_CurrentChanged(object sender, EventArgs e)
        {

        }

        private void dienNuocBindingSource_AddingNew(object sender, AddingNewEventArgs e)
        {
            if (phongBindingSource.Current != null)
            {
                var phong = (Data.Phong)phongBindingSource.Current;
                e.NewObject = new Data.DienNuoc()
                {
                    MaPhong = phong.MaPhong,Ngay= DateTime.Now, NamHoc = DateTime.Now.Year
                };
            }
            
        }
    }
}